import Taro, { Config } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { AtActivityIndicator } from 'taro-ui'

import Actions from '../../utils/actions'
import Api from '../../utils/api'
import Utils from '../../utils/utils'
import Global from '../../utils/global'

import './login.scss'

export default class IndexPage extends Taro.Component {
  constructor () {
    super(...arguments)
    this.state = {
      loading: true
    }
  }

  config: Config = {
    navigationBarTitleText: '系统登录'
  }

  //////////////////////////////////// Hooks ////////////////////////////////
  async componentWillMount(){
    Utils.debug('login/login')
    //登录
    try{
      const code = ''
      const res = await Api.request(Actions.LOGIN,{code: code})
      const data = res.data
      const token = data.list[0].token
      Api.setToken(token)
      //跳转到主页
      Taro.redirectTo({
        url:'../index'
      })
    }
    catch(e){
      Utils.error(e)
    }
  }

  //////////////////////////////////// Hooks ///////////////////////////////
  render () {
    const { loading } = this.state

    return (
      <View style='position: relative;padding:100px;'>
        {loading && <AtActivityIndicator content='正在登录...' mode='center'></AtActivityIndicator>}
      </View>
    )
  }

  //////////////////////////////////// Events ///////////////////////////////

}
